import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayServlet extends HttpServlet {

  public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {

    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    out.print("<html><head>");
    out.print("</head><body>");

    out.print("<code><pre>");
    out.print("<font color=green>ID\tFirst ");
    out.println("Name\tLast Name\n</font>");

    // debugging info

    long time1 = System.currentTimeMillis();

    // connecting to database

    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;

    try {
    // Load the JDBC driver
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);

    // Create a connection to the database
    String serverName = "127.0.0.1";
    String portNumber = "1521";
    String sid = "cs1db";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "username";
    String password = "password";
    con = DriverManager.getConnection(url, username, password);

      stmt = con.createStatement();
      rs = stmt.executeQuery("SELECT * FROM Dogs");

      // displaying records

      while(rs.next()) {
        out.print(rs.getObject(1).toString());
        out.print("\t");
        out.print(rs.getObject(2).toString());
        out.print("\t\t");
        out.print(rs.getObject(3).toString());
        out.print("\n");
        out.print(rs.getObject(4).toString());
        out.print("\n");
      }

  
    } catch (SQLException e) {
    } catch (ClassNotFoundException e) {
    } finally {
      try {
        if(rs != null) {
          rs.close();
          rs = null;
        }
        if(stmt != null) {
          stmt.close();
          stmt = null;
        }
        if(con != null) {
          con.close();
          con = null;
        }
      } catch (SQLException e) {}
    }

    out.print("</body></html>");
    out.close();
  }
}
